package io.github.loan.common.interceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import io.github.loan.users.pojo.Admins;
import io.github.loan.users.service.IUsersService;

/**
 * 管理员的验证登录拦截器
 * @author Andone
 *
 */
@Component("adminsAuthInterceptor")
public class AdminsAuthInterceptor extends HandlerInterceptorAdapter{
	@Resource
	private IUsersService usersService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		/*从session总获取admins对象
		 *判断是否为空
		*/
		HttpSession session =request.getSession();
		Admins admins=(Admins)session.getAttribute("admins");
		if(admins!=null){
			//已经登录
			return true;
		}
		/*非法访问*/
		String info="请先登录";
		session.setAttribute("info",info);
		response.sendRedirect(request.getContextPath()+"/adminsLogin.mvc");
		return false;
	}
	
}
